package forms;

import java.net.URL;
import java.util.Hashtable;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;

import models.LinkModel;

public class LinkForm {
	public TreeMap checkValid(HttpServletRequest request, LinkModel link) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String name = (String) request.getParameter("name");
		String desc = (String) request.getParameter("desc");
		String url = (String) request.getParameter("url");
		String type = (String) request.getParameter("type");
		String category = (String) request.getParameter("category");
		String keywords = (String) request.getParameter("keywords");
		String metaDesc = (String) request.getParameter("metaDesc");
		String urlReciprocal = (String) request.getParameter("urlReciprocal");
		String sourceReciprocal = (String) request.getParameter("sourceReciprocal");
		String permission = (String) request.getAttribute("permission");
		String status = (String) request.getParameter("status");
		
		if (name == null || name.equals("")) {
			check.put("name", "Name is not empty");
			link.setName(name);
		} else {
			link.setName(name);
		}
		
		if (desc == null || desc.equals("")) {
			check.put("desc", "Desc is not empty");
			link.setDesc(desc);
		} else {
			link.setDesc(desc);
		}
		
		if (url == null || url.equals("")) {
			check.put("url", "Url is not empty!");
			link.setUrl(url);
		} else {
			try {
				link.setUrl(url);
				URL sUrl = new URL(link.getUrl());
			} catch (Exception e) {
				check.put("url", "Url not valid!");
			}
		}
		
		if (type != null && type.equals("")) {
			check.put("type", "type is not empty");
		} else if (type != null) {
			link.setType(type);
		}
		
		if (category == null || category.equals("")) {
			check.put("category", "Category is not empty");
		} else {
			try {
				link.setCategory(new Long(category));
			} catch (Exception e) {
				check.put("category", "Category is a number value!");
			}
			
		}
		
		if (keywords == null || keywords.equals("")) {
			check.put("keywords", "Keywords is not empty");
			link.setKeywords(keywords);
		} else {
			link.setKeywords(keywords);
		}
		
		if (metaDesc == null || metaDesc.equals("")) {
			check.put("metaDesc", "metaDesc is not empty");
			link.setMetaDesc(metaDesc);
		} else {
			link.setMetaDesc(metaDesc);
		}
		
		if (link.getType() != null && link.getType().equalsIgnoreCase("reciprocal") && (permission == null || !permission.equalsIgnoreCase("admin"))) {
			if(urlReciprocal.equals(""))
			{
				check.put("urlReciprocal", "urlReciprocal is not empty");
			} else {
				link.setUrlReciprocal(urlReciprocal);
				if (!checkLinkReciprocal(link.getUrlReciprocal(), link.getSourceReciprocal())) {
					check.put("urlReciprocal", "Not contain HTML Reciprocal source in HTML Source link");
				}
			}
		}
		
		if (status != null && !status.equalsIgnoreCase("")) {
			link.setStatus(Integer.parseInt(status));
		}
		
		form.put("checkValid", check);
		form.put("link", link);
		
		return form;
	}
	
	private boolean checkLinkReciprocal(String urlReciprocal, String sourceReciprocal) {
		boolean isContain = false;
		try {
			String content = Jsoup.connect(urlReciprocal).get().html();
			if (content.contains(sourceReciprocal)) {
				isContain = true;
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return isContain;
	}
}
